const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
const { Command } = require("commander");
const program = new Command();
const inquirer = require("inquirer");
const prompt = inquirer.createPromptModule();

// 信息
program
  .version("0.0.1")
  .description("A simple cli")
  .usage("[command] [options]");

// list 命令，查表
const listCommand = program.command("list-table [table]").alias("list");
listCommand
  .description("列出某个表的信息，help list 显示帮助")
  .helpOption("-h, --help", "显示帮助信息")
  .action(async (table) => {
    if (!prisma[table]) {
      try {
        const result =
          await prisma.$queryRaw`select * from sqlite_master where type='table'`;
        console.log("存在的表：");
        for (const tInfo of result) {
          console.info("  ", tInfo.tbl_name);
        }
      } catch (e) {
        console.error(e);
      }
      return;
    }
    prisma[table].findMany().then((data) => {
      console.log(data);
    });
  });

// 创建文章分类
const createArticleSortCommand = program.command("create-article-sort");
createArticleSortCommand
  .description("创建文章分类，help create-article-sort 显示帮助")
  .helpOption("-h, --help", "显示帮助信息")
  .option("-n, --name <name>", "分类名称")
  .option("-d, --description <description>", "分类描述")
  .action(async () => {
    let { name, description } = createArticleSortCommand.opts();
    if (!name || typeof name !== "string") {
      const answers = await prompt([
        {
          type: "input",
          name: "name",
          message: "请输入分类名称",
        },
      ]);
      name = answers.name;
    }
    if (!description || typeof description !== "string") {
      const answers = await prompt([
        {
          type: "input",
          name: "description",
          message: "请输入分类描述",
        },
      ]);
      description = answers.description;
    }
    try {
      const sort = prisma.article_sort
        .create({
          data: {
            name,
            description,
          },
        })
        .then((data) => {
          console.log(data);
        });
      if (!sort) {
        console.error("创建失败");
      }
    } catch (e) {
      console.error(e);
    }
  });

// 创建用户
const createUserCommand = program.command("create-user");
createUserCommand
  .description("创建用户，help create-user 显示帮助")
  .helpOption("-h, --help", "显示帮助信息")
  .option("-n, --nickname <nickname>", "昵称")
  .option("-u, --username <username>", "用户名")
  .option("-p, --password <password>", "密码")
  .action(async () => {
    let { nickname, username, password } = createUserCommand.opts();
    console.log(`nickname: ${nickname}`);
    console.log(`username: ${username}`);
    console.log(`password: ${password}`);
    if (!nickname || typeof nickname !== "string") {
      const answers = await prompt([
        {
          type: "input",
          name: "nickname",
          message: "请输入昵称",
        },
      ]);
      nickname = answers.nickname;
    }
    if (!username || typeof username !== "string") {
      const answers = await prompt([
        {
          type: "input",
          name: "username",
          message: "请输入用户名",
        },
      ]);
      username = answers.username;
    }
    if (!password || typeof password !== "string") {
      const answers = await prompt([
        {
          type: "password",
          name: "password",
          message: "请输入密码",
        },
      ]);
      password = answers.password;
    }
    try {
      let user = await prisma.user.findUnique({
        where: {
          username: username,
        },
      });
      if (user) {
        console.error("用户名已存在");
        return;
      }

      user = await prisma.user.create({
        data: {
          nickname: nickname,
          username: username,
          password: password,
        },
      });
      if (!user) {
        console.error("创建用户失败");
      } else {
        console.log("创建用户成功");
        console.info(user);
      }
    } catch (e) {
      console.error(e);
      return;
    }
  });

program.parse(process.argv);
